<?php
namespace app\car\controller;
use app\AdminRest;

use app\car\model\ArticleCate;
use app\car\model\ArticleList;
use app\car\model\CarCate;
use app\car\model\CarInfo;
use app\car\model\CarList;
use app\car\model\CarSetting;
use app\shop\model\IndexCompany;
use think\App;




class AdminCar extends AdminRest
{

    protected $model;

    protected $cate_model;

    protected $site_model;



    public function __construct(App $app) {

        parent::__construct($app);

        $this->model  = new CarList();

        $this->cate_model  = new CarCate();

        $this->site_model  = new CarSetting();


    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:分类列表
     */
    public function cateList(){

        $input = $this->_param;

        $dis[] = ['status','>',-1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['pid','=',0];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->cate_model->dataList($dis,$input['limit']);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $where = [

                    'pid' => $v['id']
                ];

                $v['children'] = $this->cate_model->where($where)->where('status','>',-1)->order('top desc,id desc')->select()->toArray();

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:分类列表
     */
    public function cateSelect(){

        $input = $this->_param;

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['pid','=',0];

        $data = $this->cate_model->where($dis)->order('top desc,id desc')->select()->toArray();

        if(!empty($data)){

            foreach ($data as &$v){

                $where = [

                    'pid' => $v['id']
                ];

                $v['children'] = $this->cate_model->where($where)->where('status','=',1)->order('top desc,id desc')->select()->toArray();

            }

        }

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:添加分类
     */
    public function cateAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $data = $this->cate_model->dataAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:编辑分类
     */
    public function cateUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $data = $this->cate_model->dataUpdate($dis,$input);

        if(isset($data['status'])&&is_array($data['status'],[0,-1])){

            $where[] = ['pid','=',$input['id']];

            $where[] = ['status','>',-1];

            $this->cate_model->dataUpdate($where,['status'=>$input['status']]);

        }

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:分类详情
     */
    public function cateInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->cate_model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-19 14:06
     * @功能说明:配置列表
     */
    public function siteSelect(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $data = $this->site_model->where($dis)->order('top desc,id desc')->select()->toArray();

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-19 14:06
     * @功能说明:配置列表
     */
    public function siteList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->site_model->dataList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-19 17:21
     * @功能说明:添加配置
     */
    public function siteAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->site_model->dataAdd($input);

        return $this->success($res);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-19 17:22
     * @功能说明:编辑配置
     */
    public function siteUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $res = $this->site_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-19 17:23
     * @功能说明:配置详情
     */
    public function siteInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->site_model->dataInfo($dis);

        return $this->success($res);

    }





    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:58
     * @功能说明:汽车列表
     */
    public function carList(){

        $input = $this->_param;

        if(isset($input['status'])){

            $dis[] = ['a.status','=',$input['status']];

        }else{

            $dis[] = ['a.status','>',-1];
        }

        $dis[] = ['a.uniacid','=',$this->_uniacid];


        if(!empty($input['title'])){

            $dis[] = ['a.title','like','%'.$input['title'].'%'];
        }

//        if(isset($input['status'])){
//
//            $dis[] = ['a.status','=',$input['status']];
//        }

        if(!empty($input['cate_name'])){

            $dis[] = ['b.title','like','%'.$input['cate_name'].'%'];
        }

        $data = $this->model->adminDataList($dis,$input['limit']);

        $data['on_count']  = $this->model->where(['uniacid'=>$this->_uniacid,'status'=>1])->count();

        $data['off_count'] = $this->model->where(['uniacid'=>$this->_uniacid,'status'=>0])->count();

//        $info_model = new CarInfo();
//
//        $list = $info_model->where(['car_id'=>23])->select()->toArray();
//
//        foreach ($list as $value){
//
//            $arr[$value['key'].'-'.$value['type_key']][] = $value;
//
//        }
//
//        dump($arr);exit;

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:添加汽车
     */
    public function carAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $data = $this->model->dataAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:编辑汽车
     */
    public function carUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $data = $this->model->dataUpdate($dis,$input);

        return $this->success($input['id']);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:汽车详情
     */
    public function carInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        $cate_id[] = $data['cate_id'];

        $cate_pid = $this->cate_model->where(['id'=>$data['cate_id']])->value('pid');

        if(!empty($cate_pid)){

            array_unshift($cate_id,$cate_pid);
        }

        $data['cate_id'] = $cate_id;

        $info_model = new CarInfo();

        $dis = [

            'car_id' => $input['id']
        ];

        $data['info'] = $info_model->where($dis)->select()->toArray();

        return $this->success($data);

    }
















}
